using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace StupidSpaceThing
{
	internal class Quad
	{
		private static GraphicsDevice device;
		protected static BasicEffect quadEffect;
		private static Vector2 textureUpperLeft = new Vector2(0.0f, 0.0f);
		private static Vector2 textureUpperRight = new Vector2(1.0f, 0.0f);
		private static Vector2 textureLowerLeft = new Vector2(0.0f, 1.0f);
		private static Vector2 textureLowerRight = new Vector2(1.0f, 1.0f);
		private static VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[4];
		private static int[] indexes = new int[6];
		private static VertexDeclaration vertexDeclaration;
		
		public static void InitGraphics(GraphicsDevice _device){
			device = _device;
			quadEffect = new BasicEffect(device, null);
			quadEffect.EnableDefaultLighting();
			quadEffect.TextureEnabled = true;
			vertices[0].Position = new Vector3(1, 0, -1);
			vertices[0].TextureCoordinate = textureLowerLeft;
			vertices[1].Position = new Vector3(1, 0, 1);
			vertices[1].TextureCoordinate = textureUpperLeft;
			vertices[2].Position = new Vector3(-1, 0, -1);
			vertices[2].TextureCoordinate = textureLowerRight;
			vertices[3].Position = new Vector3(-1, 0, 1);
			vertices[3].TextureCoordinate = textureUpperRight;
			indexes[0] = 0;
			indexes[1] = 1;
			indexes[2] = 2;
			indexes[3] = 2;
			indexes[4] = 1;
			indexes[5] = 3;
			vertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements);
		}

		private Texture2D texture;
		public Texture2D Texture {
			get { return texture; }
			set { texture = value; }
		}
		public Vector3 Position = Vector3.Zero;
		public float Scale = 1;
		public Vector3? VectorScale = null;
		public Matrix Rotation = Matrix.Identity;
		public Color Color = Color.TransparentWhite;

		public Quad(Texture2D texture) {
			this.texture = texture;
		}

		public void Draw(Camera camera) {
			device.RenderState.DepthBufferEnable = false;
			if( VectorScale == null ) {
				quadEffect.World = Matrix.CreateScale(Scale) * Rotation * Matrix.CreateTranslation(Position);
			} else {
				quadEffect.World = Matrix.CreateScale(VectorScale.Value) * Rotation * Matrix.CreateTranslation(Position);
			}
			quadEffect.View = camera.View;
			quadEffect.Projection = camera.Projection;
			quadEffect.Texture = texture;
			quadEffect.AmbientLightColor = Color.ToVector3();
			device.VertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements);
			quadEffect.Begin();			
			foreach( EffectPass pass in quadEffect.CurrentTechnique.Passes ) {
				pass.Begin();
				device.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertices, 0, 4, indexes, 0, 2);
				pass.End();
			}
			quadEffect.End();
			device.RenderState.DepthBufferEnable = true;			
		}

		public void DrawOpaque(Camera camera) {
			if( VectorScale == null ) {
				quadEffect.World = Matrix.CreateScale(Scale) * Rotation * Matrix.CreateTranslation(Position);
			} else {
				quadEffect.World = Matrix.CreateScale(VectorScale.Value) * Rotation * Matrix.CreateTranslation(Position);
			}
			quadEffect.View = camera.View;
			quadEffect.Projection = camera.Projection;
			quadEffect.Texture = texture;
			quadEffect.AmbientLightColor = Color.ToVector3();
			device.VertexDeclaration = vertexDeclaration;
			quadEffect.Begin();
			foreach( EffectPass pass in quadEffect.CurrentTechnique.Passes ) {
				pass.Begin();
				device.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertices, 0, 4, indexes, 0, 2);
				pass.End();
			}
			quadEffect.End();
		}
	}
}
